// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PollAction.java

package com.iphaeton.lms.action.poll;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.poll.IPollService;
import com.iphaeton.lms.pojo.poll.Option;
import com.iphaeton.lms.pojo.poll.Poll;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;

// Referenced classes of package com.iphaeton.lms.action.poll:
//            PollBizSupportAction

public class PollAction extends PollBizSupportAction
{

    public PollAction()
    {
    }

    public ActionForward addPoll(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        qaf = (DynaActionForm)actionForm;
        Option selOption = null;
        String id = request.getParameter("poll");
        String option_id = request.getParameter("test");
        List pollList;
        try
        {
            pollList = poll.queryPollOption(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            return mapping.findForward("failure");
        }
        for(Iterator it = pollList.iterator(); it.hasNext();)
        {
            Option tmpObj = (Option)it.next();
            if(option_id.equals(tmpObj.getId()))
                selOption = tmpObj;
        }

        try
        {
            poll.count(selOption);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return showPoll(mapping, actionForm, request, response);
    }

    public ActionForward showPoll(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        qaf = (DynaActionForm)actionForm;
        String id = (String)qaf.get("poll");
        List polllist = null;
        List list = null;
        try
        {
            polllist = poll.queryPoll();
            list = poll.queryPollOption(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("poll_list", list);
        request.setAttribute("poll", polllist);
        return mapping.findForward("pollList");
    }

    public ActionForward resultPoll(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        List polllist = null;
        List list = null;
        try
        {
            polllist = poll.queryPoll();
            Iterator pollOption = polllist.iterator();
            String id;
            for(id = ""; pollOption.hasNext(); id = ((Poll)pollOption.next()).getId());
            list = poll.queryPollOption(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("poll_list", list);
        request.setAttribute("poll", polllist);
        return mapping.findForward("poll_list");
    }

    public ActionForward queryPoll(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        List pollList = null;
        List optionList = null;
        int totalCount = 0;
        Set opSet = null;
        try
        {
            pollList = poll.queryPoll();
            if(pollList.size() > 0)
            {
                opSet = ((Poll)pollList.get(0)).getOption();
                optionList = poll.queryPollOption(((Poll)pollList.get(0)).getId());
                for(Iterator it = opSet.iterator(); it.hasNext();)
                {
                    int count = ((Option)it.next()).getCount().intValue();
                    totalCount += count;
                }

            }
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("count", (new StringBuffer(String.valueOf(totalCount))).toString());
        request.setAttribute("poll", pollList);
        request.setAttribute("option", optionList);
        return mapping.findForward("poll_result");
    }

    public ActionForward addPolloption(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        qaf = (DynaActionForm)actionForm;
        HashMap choice_hm = new HashMap();
        choice_hm.put("1", "A");
        choice_hm.put("2", "B");
        choice_hm.put("3", "C");
        choice_hm.put("4", "D");
        choice_hm.put("5", "E");
        choice_hm.put("6", "F");
        choice_hm.put("7", "G");
        choice_hm.put("8", "H");
        choice_hm.put("9", "I");
        choice_hm.put("10", "J");
        String biaoti = (String)qaf.get("BiaoTi");
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String mDateTime = formatter.format(cal.getTime());
        Poll poll = new Poll();
        poll.setCreate_time(mDateTime);
        poll.setState(new Integer(1));
        poll.setTitle(biaoti);
        poll.setValid(new Integer(0));
        try
        {
            getPoll().add(poll);
            String pollid = poll.getId();
            String choice = (String)qaf.get("choice");
            int i_choice = Integer.parseInt(choice);
            for(int i = 1; i <= i_choice; i++)
            {
                String s = String.valueOf(i);
                String s_value = (String)choice_hm.get(s);
                String choice_value = (String)qaf.get(s_value);
                Option option = new Option();
                option.setCount(new Integer(0));
                option.setText(choice_value);
                option.setPoll_id(pollid);
                getPoll().addOption(option);
            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("poll_table");
    }

    public ActionForward pollTable(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 0;
        int pageSize = 10;
        List polllist = null;
        String strPage = request.getParameter("page");
        try
        {
            if(strPage == null || strPage.trim().equals(""))
            {
                currentPage = 1;
                pageInfo = getPoll().findPoll(pageSize);
                pageInfo.setPage(currentPage);
            } else
            if(strPage != null && !strPage.trim().equals(""))
            {
                currentPage = Integer.parseInt(strPage);
                pageInfo = (Pagination)request.getSession().getAttribute("pageInfo");
                pageInfo.setPage(currentPage);
            } else
            {
                pageInfo = (Pagination)request.getSession().getAttribute("pageInfo");
            }
            request.getSession().setAttribute("pageInfo", pageInfo);
            polllist = getPoll().getSpecifiedPoll(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("poll", polllist);
        return mapping.findForward("poll_form");
    }

    public ActionForward pollValid(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        qaf = (DynaActionForm)actionForm;
        String pollid = (String)qaf.get("pollid");
        try
        {
            poll.valid(pollid);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("poll_table");
    }

    public ActionForward pollOpen(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        qaf = (DynaActionForm)actionForm;
        String pollid = (String)qaf.get("pollid");
        try
        {
            poll.open(pollid);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("poll_table");
    }

    public ActionForward removePoll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id[] = request.getParameter("id").split(";");
        Poll p = null;
        try
        {
            for(int i = 0; i < id.length; i++)
            {
                p = getPoll().findPollById(id[i]);
                poll.removePoll(p);
            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("poll_table");
    }

    public ActionForward pollMain(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        List polllist = null;
        List list = null;
        try
        {
            polllist = poll.queryPoll();
            Iterator pollOption = polllist.iterator();
            String id;
            for(id = ""; pollOption.hasNext(); id = ((Poll)pollOption.next()).getId());
            list = poll.queryPollOption(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("poll_list", list);
        request.setAttribute("poll", polllist);
        return mapping.findForward("poll_first");
    }

    private DynaActionForm qaf;
    private Pagination pageInfo;
}
